class Graph extends Control
{
  ArrayList items;
  int maxVal;
  int selection = -1;
  public Graph()
  {
    backcolor = color(#dadada);
    forecolor = color(#ff9000);
    width = scrW;
    height = scrH - 100;
  }
  
  public void draw()
  {
    width = scrW;
    height = scrH - 100;
    int areaheight = height - 40;
    int areawidth = width - 40;
    noStroke();
    fill(backcolor);
    rect(getX(), getY(), width, height);
    fill(#efefef);
    strokeWeight(1);
    stroke(#bbbbbb);
    rect(getX() + 20, getY() + 20, areawidth, areaheight);
    
    float posx = 0;
    float posy = 0;
    int tempMax = maxVal;
    tempMax += 5 - (tempMax % 5);
    
    for(int i = 0; i <= 5; i++)
    {
      line(getX() + 20, getY() + 20 + posy, getX() + 20 + areawidth, getY() + 20 + posy);
      fill(50);
      textFont(font1);
      text(""+(tempMax * (5-i)/5), getX() + 5, getY() + 25 + posy);
      posy += areaheight / 5;
    }
    posx = 0; posy = 0;
    for(int i = 1; i < tempMax; i++)
    {
      if(i % (tempMax / 5) == 0) continue;
      posy = i * areaheight / tempMax;
      stroke(#d3d3d3);
      line(getX() + 20, getY() + 20 + posy, getX() + 20 + areawidth, getY() + 20 + posy);
    }
    posx = 0;
    posy = 0;
    float posdif = (float)areawidth / (items.size()-1);
    float oldx = 0;
    float oldy = 0;
    for(int i = 0; i < items.size(); i++)
    {
      posx = (areawidth-1) * i / (items.size() - 1);
      Integer value = (Integer)items.get(i);
      stroke(forecolor);
      strokeWeight(2);
      if(tempMax > 0)
        posy = areaheight - areaheight * value / (tempMax);
      else posy = areaheight;
      if(i > 0)
      {
        noStroke();
        fill(#ffbd68);
        beginShape(TRIANGLE_STRIP);
        vertex(getX() + oldx + 21, getY() + oldy + 20);
        vertex(getX() + posx + 21, getY() + posy + 20);
        vertex(getX() + posx + 21, getY() + areaheight + 20);
        vertex(getX() + oldx + 21, getY() + areaheight + 20);
        vertex(getX() + oldx + 21, getY() + oldy + 20);
        endShape();
        stroke(forecolor);
        line(getX() + oldx + 21, getY() + oldy + 20, getX() + posx + 21, getY() + posy + 20);
      }
      oldx = posx;
      oldy = posy;
      //posx += posdif;
    }
    strokeWeight(1);
    long minT = db.qryMin == 0 ? db.minTime.getTime() : db.qryMin;
    long maxT = db.qryMax == 0 ? db.maxTime.getTime() : db.qryMax;
    long interval = (maxT - minT) / items.size();
    if(selection >= 0 && selection < items.size() && tempMax != 0 && items.size() > 0)
    {
      fill(forecolor);
      stroke(255);
      int value = (Integer)items.get(selection);
      int selx = getX() + 21 + (areawidth-1) * selection / (items.size() - 1);
      int sely = getY() + 20 + areaheight - areaheight * value / tempMax;
      ellipse(selx, sely, 12, 12);
      long dateTime1 = minT + selection * interval;
      long dateTime2 = minT + (selection + 1) * interval;
      Date date1 = new Date(dateTime1);
      Date date2 = new Date(dateTime2);
      String out1 = "" + value;
      String out2 = dateformat.format(date1) + " - " + dateformat.format(date2);
      fill(#fefeff);
      stroke(#767676);
      textFont(font1);
      int rw = max((int)textWidth(out1), (int)textWidth(out2)) + 10;
      int rh = 40;
      int rx = selx;
      if(rx < 0) rx = 0;
      else if(rx + rw > width) rx = width - rw;
      int ry = sely - rh - 10;
      if(ry < 0) ry = 0;
      rect(rx, ry, rw, rh);
      fill(forecolor);
      textFont(font1bold);
      text(out1, rx + 5, ry + 17);
      fill(#767676);
      textFont(font1);
      text(out2, rx + 5, ry + 30);
    }
    fill(#767676);
    textFont(font1);
    long tInt = interval;
    int days = (int)(tInt / 86400000);
    tInt = tInt - (days * 86400000);
    int hours = (int)(tInt / 3600000);
    tInt = tInt - (hours * 3600000);
    int mins = (int)(tInt / 60000);
    String strInterval = "";
    if(days > 0) strInterval = strInterval + days + "d ";
    if(hours > 0) strInterval = strInterval + hours + "h ";
    if(mins > 0) strInterval = strInterval + mins + "m";
    text("Interval:" + strInterval, getX() + 25, getY() + 40);
  }
  
  public boolean getEvent(int mx, int my, int mstate)
  {
    selection = -1;
    if(!mouseOver(mx, my))
      return false;
    
    if(mx >= 21 && mx < width - 21 && my >= 21 && my < height - 21 && items.size() > 1)
    {
      float w = (width - 41) / (items.size() - 1);
      selection = (int)((mx - 21) / w);
      selection = (int)((mx - 21  + w/2) * (items.size() - 1) / (width - 41));
    }
    
    return false;
  }
}
